package band.wukong.practice.webservice.kh.sample13.nongen;

import band.wukong.practice.webservice.kh.sample13.CalcService;
import band.wukong.practice.webservice.kh.sample13.CalcServiceImplService;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * 注意： Add和AddResponse中@XmlType.name和该Class的name不能一样，所以都改成了xxx666
 *       否则会报如下错误：
 *       两个类具有相同的 XML 类型名称 "{http://www.wukong.band/wkp/webservice/kh/sample13/}add"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。
 *
 * @author wukong(wukonggg@139.com)
 */
public class ClientTest {

    @Test
    public void jaxws() {
        CalcService service = new CalcServiceImplService().getCalcServiceImplPort();
        int result = service.add(1,1);
        Assert.assertEquals(result, 2);
    }

    @Test
    public void cxfInModeContractFirst() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(CalcConst.SERVICE_URL);
        factory.setServiceClass(CalcService.class);

        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.getInInterceptors().add(new LoggingInInterceptor());

        /*
            使用interceptor的另外一种方式，CalcService加注解：
            @OutInterceptors(interceptors = {"band.wukong.practice.webservice.kh.sample13.nongen.ClientOutboundObjectLicenseInterceptor"})
         */
        factory.getOutInterceptors().add(new ClientOutboundObjectLicenseInterceptor());

        CalcService service = (CalcService) factory.create();
        int result = service.add(1,1);
        Assert.assertEquals(result, 2);

    }

}
